const mongoose = require('mongoose')
const Schema = mongoose.Schema
let counter = 1
let CountedId = { type: Number, default: () => counter++ }
const usersSchema = new Schema(
  {
    accountId: CountedId,
    account: {
      type: String,
      required: true
    },
    passwd: {
      type: String,
      required: true
    },
    name: {
      type: String
    },
    gender: {
      type: Boolean
    },
    phone: {
      type: String
    },
    remark: {
      type: String
    },
    createTime: {
      type: String
    },
    updataTime: {
      type: String,
      default: Date.now()
    }
  },
  { collection: 'users', versionKey: false }
)
const userModel = mongoose.model('users', usersSchema)
module.exports = userModel
userModel
  .find({ id: { $gt: 0 } })
  .sort({ id: -1 })
  .then(([first, ...others]) => {
    if (first) counter = first.id + 1
  })
